'''
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with self work for additional information
 * regarding copyright ownership.  The ASF licenses self file
 * to you under the Apache License, Version 2.0 (the
 * "License") you may not use self file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http:#www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
'''
from base.java.HashMap import HashMap
from base.rdf.RDFReaderF import RDFReaderF
from base.rdf.impl.NTripleReader import NTripleReader
from base.rdfxml.xmlinput.RDFXMLReader import RDFXMLReader
from base.shared.JenaException import JenaException


# import java.util.LinkedHashMap;
# import java.util.Map;

# import org.apache.jena.atlas.logging.Log;
# import org.apache.jena.rdf.model.RDFReaderF;
# import org.apache.jena.rdf.model.RDFReaderI;
# import org.apache.jena.shared.JenaException;
# import org.apache.jena.shared.NoReaderForLangException;

# Object,
class RDFReaderFImpl(RDFReaderF):
    # public static final String
    DEFAULTLANG = "RDF/XML"
    # param 
    # result static Map<String, Class<? extends RDFReaderI>> custom = new LinkedHashMap<>(self)
    custom = HashMap()

    # private static RDFReaderF
    rewiredAlternative = None

    '''
     * Rewire to use an external RDFReaderF (typically, RIOT).
     * Set to null to use old jena-core setup.
     *
     * @param other
    '''

    # param RDFReaderF other
    # result static void
    def alternative(self, other):
        self.rewiredAlternative = other

    '''
     * Creates RDFReaderFImpl
    '''

    # param
    def __init__(self):
        self.reset()

    # @Override
    # param 
    # result RDFReaderI
    def getReader(self):
        return self.getReaderWithLang(None)

    # @Override
    # param String lang
    # result RDFReaderI
    def getReaderWithLang(self, lang):
        # Jena model.read rule for defaulting.
        if (lang is None or lang.equals("")):
            lang = self.DEFAULTLANG
        # if RIOT ->
        if self.rewiredAlternative is not None:
            return self.rewiredAlternative.getReader(lang)
        # Class<? extends RDFReaderI>
        c = self.custom.get(lang)
        if (c is None):
            # raise NoReaderForLangException("Reader not found: " + lang)
            raise JenaException("Reader not found: " + lang)

        try:
            return c.getConstructor().newInstance()
        except Exception as e:
            raise JenaException(e)

    # static:
    # static initializer - set default readers

    # param
    # result static void
    def reset(self):
        # Class<? extends RDFReaderI>
        # rdfxmlReader = org.apache.jena.rdfxml.xmlinput.RDFXMLReader.class
        rdfxmlReader = RDFXMLReader.__class__
        # Class<? extends RDFReaderI>
        ntReader = NTripleReader.__class__
        # Turtle moved to test-only

        self.custom.put("RDF", rdfxmlReader)
        self.custom.put("RDF/XML", rdfxmlReader)
        self.custom.put("RDF/XML-ABBREV", rdfxmlReader)

        self.custom.put("N-TRIPLE", ntReader)
        self.custom.put("N-TRIPLES", ntReader)
        self.custom.put("N-Triples", ntReader)

    # param String lang
    # result static String
    def currentEntry(self, lang):
        # Class<? extends RDFReaderI>
        oldClass = self.custom.get(lang)
        if (oldClass is not None):
            return oldClass.getName()
        else:
            return None

    # param String lang
    # result static String
    def remove(self, lang):
        if (self.rewiredAlternative is not None):
            print("Rewired RDFReaderFImpl - configuration changes have no effect on reading")
            # Log.error(RDFReaderFImpl.class, "Rewired RDFReaderFImpl - configuration changes have no effect on reading")

        # String
        oldClassName = self.currentEntry(lang)
        self.custom.remove(lang)
        return oldClassName
